package com.apache.bid.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.apache.api.manager.ProxyManager;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.api.vo.ResultMsg;
import com.apache.bid.common.cache.BidCacheFactory;
import com.apache.bid.common.cache.BidCacheManager;
import com.apache.bid.controller.BaseSupperAction;
import com.apache.bid.entity.BidApply;
import com.apache.bid.manager.BidApplyManager;
import com.apache.bid.manager.UnityBaseManager;
import com.apache.cache.util.Validator;
import com.apache.tools.StrUtil;
import com.apache.uct.common.PBOSSOTools;

@Controller
@RequestMapping("/admin/apply")
public class BidApplyAction extends BaseSupperAction<BidApply> {

	@Autowired
	private BidApplyManager bidApplyManager;
	@Autowired
	private ProxyManager wbdProxyManager;

	@Override
	public ResultMsg saveInfo(HttpServletRequest request, HttpServletResponse response, BidApply apply) {
		ResultMsg rmsg = new ResultMsg("T", "操作成功！");//用于封装结果信息
		response.setContentType("text/html");

		ParamsVo<BidApply> vo = this.getParamsVo(request);//初始化参数vo对象，如果是登陆用户则获取用户信息及日志

		ParamsVo<BidApply> applyVo = new ParamsVo<BidApply>();
		BidApply applyOld = new BidApply();
		applyOld.setUserId(apply.getUserId());
		if (Validator.isNotNull(apply.getItemId()) && !"0".equals(apply.getItemId())) {
			applyOld.setItemId(apply.getItemId());
		} else {
			applyOld.setSessionId(apply.getSessionId());
		}
		applyVo.setObj(applyOld);
		List<BidApply> applyDbList = (List<BidApply>) bidApplyManager.getList(applyVo);
		if (!Validator.isEmpty(applyDbList) && applyDbList.size() > 0) {
			rmsg = new ResultMsg("F", "此用户已设置成竞买人");
			return rmsg;
		}
		vo.setParams("loginUser", PBOSSOTools.getLoginUser(request));//存放当前登录用户
		vo.setObj(apply);
		vo.setKey("saveApply");
		ResultEntity entity = (ResultEntity) wbdProxyManager.saveInvoke(vo);
		String id = String.valueOf(entity.getEntity());
		if (Validator.isNull(id)) {
			rmsg = new ResultMsg("F", "新增竞买人失败，请重试!");
		}

		rmsg = new ResultMsg("T", "添加竞买人完成");
		return rmsg;
	}

	@RequestMapping(value = "/compareApply", method = RequestMethod.GET)
	public ModelAndView compareItem(HttpServletRequest request, String itemId, String sessionId, String userName) {
		ModelAndView view = new ModelAndView("/admin/viewJsp/compareApply");
		if (StrUtil.isNull(itemId) || StrUtil.isNull(userName)) {
			view = new ModelAndView("error/error");
			view.addObject("errors", "缺少必要的参数，itemId或userName为空!");
			this.logger.error("缺少必要的参数，itemId或userName为空!");
			return view;
		}
		if ("0".equals(itemId) && StrUtil.isNull(sessionId)) {
			view = new ModelAndView("error/error");
			view.addObject("errors", "缺少必要的参数，sessionId为空!");
			this.logger.error("缺少必要的参数，sessionId为空!");
			return view;
		}

		//查询标的缓存对象
		BidCacheManager cache = BidCacheFactory.getInstance().getCacheManager("apply");
		String key = cache.apply_prefix;
		//查询标的数据库对象
		ParamsVo<BidApply> vo = new ParamsVo<BidApply>();
		BidApply ba = new BidApply();
		ba.setUserName(userName);
		if ("0".equals(itemId)) {
			ba.setSessionId(sessionId);
			key += userName + "_" + sessionId;
		} else {
			ba.setItemId(itemId);
			key += userName + "_" + itemId;
		}
		vo.setMethodKey("singleApplyByAttr");
		vo.setObj(ba);
		BidApply bidApply = (BidApply) bidApplyManager.execute(vo);
		if (bidApply == null) {
			view = new ModelAndView("error/error");
			view.addObject("errors", "查不到apply信息，itemId=" + itemId + ";---sessionId=" + sessionId);
			return view;
		} else {
			view.addObject("apply", bidApply);
		}

		boolean mark = cache.checkCacheObject(key);
		if (mark) {
			BidApply cacheVo = (BidApply) cache.getCacheCloneByKey(key);
			view.addObject("applyCache", cacheVo);
		}

		return view;
	}

	@Override
	protected UnityBaseManager<BidApply> getBaseManager() {
		return bidApplyManager;
	}

	@Override
	protected String getCommBoxValue(BidApply item, String type) {
		// TODO Auto-generated method stub
		return null;
	}

}
